package com.pig4cloud.pig.homecenter.ws;

import com.pig4cloud.plugin.websocket.holder.SessionKeyGenerator;
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

@Service
public class SecuritySessionKeyGenerator implements SessionKeyGenerator {
	public Object sessionKey(WebSocketSession webSocketSession) {
		BearerTokenAuthentication authentication = (BearerTokenAuthentication)webSocketSession.getPrincipal();
		String clientId = String.valueOf(authentication.getTokenAttributes().get("clientId"));
		Object obj = webSocketSession.getAttributes().get("USER_KEY_ATTR_NAME");
		String key = clientId + "_" + String.valueOf(obj);
		return key;
	}

	public SecuritySessionKeyGenerator() {

	}
}
